# -*- coding: utf-8 -*-
import math

def readp():
    f = open("prime.txt")
    l = f.readlines()
    f.close

    result = []
    for stra in l:
        a = stra.split(' ')
        result = map(int,a)
    return result

def getn(n):
    ret = []
    for x in primes:
        if x == 5:
            continue
        if x == 2:
            continue
        if x < n:
            ret.append(x)
        else:
            break
    return ret

primes = readp()

def isPrime(n):
    if n <= 1:
        return False
    for i in range(2, int(math.sqrt(n)) + 1):
        if n % i == 0:
            return False
    return True

def panduanab(a,b):
    stra = str(a)
    strb = str(b)

    strab = stra + strb
    strba = strb + stra

    return isPrime(int(strab)) and isPrime(int(strba))

def testset():
    pln = getn(674)
    print pln
    lenpln = len(pln)
    print lenpln
    for a in xrange(lenpln):
        for b in xrange(a+1,lenpln):
            for c in xrange(b+1,lenpln):
                for d in xrange(c+1,lenpln):
                    if panduanab(pln[a],pln[b]) and \
                       panduanab(pln[a],pln[c]) and \
                       panduanab(pln[a],pln[d]) and \
                       panduanab(pln[b],pln[c]) and \
                       panduanab(pln[b],pln[d]) and \
                       panduanab(pln[c],pln[d]):
                        print pln[a],pln[b],pln[c],pln[d]


def test2():
    pln = getn(10000)
    lenpln = len(pln)

##    print pln
    print u'pln长度',lenpln
    start = time.time()
    for a in xrange(lenpln):
        for b in xrange(a+1,lenpln):
            if panduanab(pln[a],pln[b]):
                for c in xrange(b+1,lenpln):
                    if panduanab(pln[a],pln[c]) and panduanab(pln[b],pln[c]):
                        for d in xrange(c+1,lenpln):
                            if panduanab(pln[a],pln[d]) and \
                               panduanab(pln[b],pln[d]) and \
                               panduanab(pln[c],pln[d]) :
                                for e in xrange(d+1,lenpln):
                                    if panduanab(pln[a],pln[e]) and \
                                       panduanab(pln[b],pln[e]) and \
                                       panduanab(pln[c],pln[e]) and \
                                       panduanab(pln[d],pln[e]) :

                                        ppp = [pln[a],pln[b],pln[c],pln[d],pln[e]]
                                        print ppp,sum(ppp)
                                        print('Execution time: {0} seconds'.format(time.time() - start))
                                        exit()
def main():
    test2()

if __name__ == '__main__':
    import time
    start = time.time()
    main()
    print('Execution time: {0} seconds'.format(time.time() - start))
